package com.marktotop.utils;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;

/**
 * 根据dom4j规则生成对应的xml文件
 *       主要使用API：
 *              Document
 *              Element
 *              Attribute
 */
public class CreateXml {

    public static void main(String[] args) {

        createXMLProject(new File("F:\\IdeaProjects\\marktotop_xml\\src\\main\\resources\\student.xml"));
    }

    private static void createXMLProject(File file) {

        try {

            // 创建一个Document实例
            Document document = DocumentHelper.createDocument();

            // 添加根节点
            Element students = document.addElement("students");

            // 在根节点下添加第一个子节点
            Element student1 = students.addElement("student");
            student1.addElement("id").addText("201800001");
            student1.addElement("name").addText("张三");
            student1.addElement("sex").addText("男");
            student1.addElement("age").addText("23");
            student1.addElement("gradeName").addText("2018级");
            student1.addElement("major").addText("软件工程");
            student1.addElement("contact").addText("13891231323");
            student1.addElement("address").addText("陕西省咸阳市XXX地区");

            // 在根节点下添加第二个子节点
            Element student2 = students.addElement("student");
            student2.addElement("id").addText("201800002");
            student2.addElement("name").addText("李四");
            student2.addElement("sex").addText("男");
            student2.addElement("age").addText("22");
            student2.addElement("gradeName").addText("2018级");
            student2.addElement("major").addText("计算机科学与技术");
            student2.addElement("contact").addText("13112363823");
            student2.addElement("address").addText("陕西省宝鸡市XXX地区");

            // 在根节点下添加第三个子节点
            Element student3 = students.addElement("student");
            student3.addElement("id").addText("201800003");
            student3.addElement("name").addText("王五");
            student3.addElement("sex").addText("女");
            student3.addElement("age").addText("23");
            student3.addElement("gradeName").addText("2018级");
            student3.addElement("major").addText("物联网工程");
            student3.addElement("contact").addText("18121261837");
            student3.addElement("address").addText("陕西省西安市XXX地区");

            // 在根节点下添加第四个子节点
            Element student4 = students.addElement("student");
            student4.addElement("id").addText("201800004");
            student4.addElement("name").addText("赵六");
            student4.addElement("sex").addText("男");
            student4.addElement("age").addText("21");
            student4.addElement("gradeName").addText("2018级");
            student4.addElement("major").addText("能源专业");
            student4.addElement("contact").addText("13221372257");
            student4.addElement("address").addText("浙江省绍兴市XXX地区");

            // 在根节点下添加第五个子节点
            Element student5 = students.addElement("student");
            student5.addElement("id").addText("201800005");
            student5.addElement("name").addText("马七");
            student5.addElement("sex").addText("女");
            student5.addElement("age").addText("23");
            student5.addElement("gradeName").addText("2018级");
            student5.addElement("major").addText("美术专业");
            student5.addElement("contact").addText("15132619837");
            student5.addElement("address").addText("北京市XXX地区");


            // xml格式化样式  默认样式
//            OutputFormat format = OutputFormat.createPrettyPrint();

            // 自定义xml样式
            OutputFormat format = new OutputFormat();
            format.setIndentSize(2);  // 行缩进
            format.setNewlines(true); // 一个结点为一行
            format.setTrimText(true); // 去重空格
            format.setPadText(true);
            format.setNewLineAfterDeclaration(false); // 放置xml文件中第二行为空白行

            // 写入文件
            XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
            writer.write(document);
            System.out.println("dom4j CreateDom4j success!");

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
